import { jiweiAxios } from "./axios.js";
import { STATUS, URL } from "./config.js";
import iconv from "iconv-lite";
import cheerio from "cheerio";
import { commonUtil } from "../../utils/index.js";

async function getBaseParam() {
  const BASE_PARAM = {
    __VIEWSTATE: "",
    __VIEWSTATEGENERATOR: "",
    __EVENTVALIDATION: "",
  };
  const res = await jiweiAxios({
    url: URL.BASE_PARAM,
    responseType: "arraybuffer",
    params: {
      pi: Math.random()
    }
  });
  const BASE_HTML = iconv.decode(res.data, "gb2312");
  // console.log(res)
  // console.log(BASE_HTML)
  const $BASE_HTML = cheerio.load(BASE_HTML);
  BASE_PARAM.__VIEWSTATE = $BASE_HTML("#__VIEWSTATE").val();
  BASE_PARAM.__VIEWSTATEGENERATOR =
    $BASE_HTML("#__VIEWSTATEGENERATOR").val() || "";
  BASE_PARAM.__EVENTVALIDATION = $BASE_HTML("#__EVENTVALIDATION").val();

  const BASE_PARAM1 = {
    __VIEWSTATE: "",
    __VIEWSTATEGENERATOR: "",
    __EVENTVALIDATION: "",
  };
  const res1 = await jiweiAxios({
    url: URL.BASE_PARAM1,
    responseType: "arraybuffer",
    method: "post",
  });
  const BASE_HTML1 = iconv.decode(res1.data, "gb2312");
  const $BASE_HTML1 = cheerio.load(BASE_HTML1);
  BASE_PARAM1.__VIEWSTATE = $BASE_HTML1("#__VIEWSTATE").val();
  BASE_PARAM1.__VIEWSTATEGENERATOR =
    $BASE_HTML1("#__VIEWSTATEGENERATOR").val() || "";
  BASE_PARAM1.__EVENTVALIDATION = $BASE_HTML1("#__EVENTVALIDATION").val();

  return { BASE_PARAM, BASE_PARAM1 };
}

async function checkAnnualExisted({ person, BASE_PARAM1 }) {
  const ids = {
    PerrecordID: "",
    record: "",
    Chkrecordid: "",
  };
  person = { ...person };
  const dic = {
    __EVENTTARGET: "btnChaxun",
    __EVENTARGUMENT: "",
    __VIEWSTATE: BASE_PARAM1.__VIEWSTATE,
    ddlTiaojian: "IDCARDNO",
    txtTiaojian: person.IDNum,
    hidPersonID: "",
    hidPrePageFrom: "",
    hdnReturnJson: "",
    hdnParentPerID: "",
    __EVENTVALIDATION: BASE_PARAM1.__EVENTVALIDATION,
  };

  const res = await jiweiAxios({
    url: URL.ANNUAL_CHECK_EXISTED,
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
    },
    method: "post",
    data: commonUtil.jsonToURLString(dic),
    params: {
      pi: "0.7421405143046713",
    },
    responseType: "arraybuffer",
  });
  const CHECK_EXISTED = iconv.decode(res.data, "gb2312");

  if (CHECK_EXISTED.includes("没有查询记录")) {
    person.STATUS = STATUS.NOT_BUILD;
    return { person, ids, isExisted: false };
  }

  const $CHECK_EXISTED = cheerio.load(CHECK_EXISTED);
  // console.log(CHECK_EXISTED, '@@@@');
  const DataGridItem = $CHECK_EXISTED("tr .DataGridItem");
  if (DataGridItem?.children?.length > 0) {
    // SelectZhi('2075731','zrl','01','440105199204194215','13795204')
    const str = $CHECK_EXISTED(".DataGridItem a").attr("onclick");
    if (!str) {
      person.STATUS = STATUS.NOT_BUILD;
      return { person, ids, isExisted: false };
    }
    const arr = str
      .match(/SelectZhi\((.*)\)/)[1]
      .replace(/'/g, "")
      .split(",");
    [ids.PerrecordID, , , , ids.record] = arr;
    // await this.select();
    // OPTIONS.EDIT_MODE = Boolean(ids.Chkrecordid);
    // console.log(ids, OPTIONS.EDIT_MODE);
    return { person, ids, isExisted: true };
  } else {
    // console.log(`${person.IDNum}, ${person.name}没有建档`)
    person.STATUS = STATUS.NOT_BUILD;
    return { person, ids, isExisted: false };
  }
}

export { getBaseParam, checkAnnualExisted };
